using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Threading;
using System.Diagnostics;
using System.Collections;

using System.Runtime.Serialization;
using System.Runtime.Versioning;
using System.Net.Sockets;
using BrainTechLLC;



namespace Mud.EmlenMudMain
{
    [Serializable]
    public class SocialData : GameThingBaseClass, IComparable<SocialData>, IComparable, ISocialData
    {

        public SocialData() { }

        private string socialname = "";

        public string Socialname
        {
            get { return socialname; }
            set { socialname = value; }
        }
        private string char_no_arg = "";

        public string Char_no_arg
        {
            get { return char_no_arg; }
            set { char_no_arg = value; }
        }
        private string others_no_arg = "";

        public string Others_no_arg
        {
            get { return others_no_arg; }
            set { others_no_arg = value; }
        }
        private string char_found = "";

        public string Char_found
        {
            get { return char_found; }
            set { char_found = value; }
        }
        private string others_found = "";

        public string Others_found
        {
            get { return others_found; }
            set { others_found = value; }
        }
        private string vict_found = "";

        public string Vict_found
        {
            get { return vict_found; }
            set { vict_found = value; }
        }
        private string char_not_found = "";

        public string Char_not_found
        {
            get { return char_not_found; }
            set { char_not_found = value; }
        }
        private string char_auto = "";

        public string Char_auto
        {
            get { return char_auto; }
            set { char_auto = value; }
        }
        private string others_auto = "";

        public string Others_auto
        {
            get { return others_auto; }
            set { others_auto = value; }
        }

        [NonSerialized]
        public int nUses = 0;

        public int NUses
        {
            get { return nUses; }
            set { nUses = value; }
        }

        public int CompareTo(Object obj)
        {
            if (obj is SocialData)
            {
                SocialData soc = (SocialData)obj;
                return this.nUses.CompareTo(soc.nUses);
            }
            else return 0;
        }

        // This allows Socials to be sorted by use so we quickly find
        // the most commonly-used socials
        public int CompareTo(SocialData soc)
        {
            return this.nUses.CompareTo(soc.nUses);
        }

        public int CompareTo(ISocialData soc)
        {
            return this.nUses.CompareTo(soc.NUses);
        }

        public static void Load(int inVersion)
        {
            //if (SocialData.SerializeSocialsIn(inVersion, mud) == false)
            {
                load_old_format_socials();
                if (MiscConstants.DataStorageFormat == DataStorageFormatTypes.CompressedSerialization)
                    SocialData.Save(inVersion);
            }
        }

        public static void Save(int outVersion)
        {
            SerializeSocialsOut(outVersion);
        }



        public static void load_old_format_socials()
        {
            char[] spc = new char[1];
            spc[0] = ' ';
            FileStream fs = null;
            try
            {
                fs = new FileStream(EmlenMud.HostPathnameToMudDirectory + "\\" + MiscConstants.DataFileDirectory + "\\socials.dat", FileMode.Open, FileAccess.Read);
            }
            catch (IOException ex) { EmlenMud.LogError("Error reading socials.dat: ", ex, LogType.Always); return; }

            StreamReader sr = new StreamReader(fs);
            string szLine = sr.ReadLine().Trim();
            SocialData s = null;
            while (szLine != "END" && szLine != "#END" && sr.EndOfStream == false)
            {
                string[] rgsz = szLine.Split(spc, 2, StringSplitOptions.RemoveEmptyEntries);
                if (rgsz.Length > 0)
                {
                    string szWord = rgsz[0];

                    if (szWord == "SocialName")
                    {
                        if (s != null)
                        {
                            SocialData.AddSocial(s);
                        }
                        s = new SocialData();
                        s.socialname = rgsz[1].TrimEnd('~').ToUpper();
                    }
                    else if (szWord == "CharNoArg") { s.char_no_arg = rgsz[1].TrimEnd('~'); }
                    else if (szWord == "OtherNoArg") { s.others_no_arg = rgsz[1].TrimEnd('~'); }
                    else if (szWord == "CharFound") { s.char_found = rgsz[1].TrimEnd('~'); }
                    else if (szWord == "OtherFound") { s.others_found = rgsz[1].TrimEnd('~'); }
                    else if (szWord == "VictFound") { s.vict_found = rgsz[1].TrimEnd('~'); }
                    else if (szWord == "CharNFound") { s.char_not_found = rgsz[1].TrimEnd('~'); }
                    else if (szWord == "CharSelf") { s.char_auto = rgsz[1].TrimEnd('~'); }
                    else if (szWord == "OtherSelf") { s.others_auto = rgsz[1].TrimEnd('~'); }
                }
                szLine = sr.ReadLine().Trim();
            }
            if (s != null)
            {
                AddSocial(s);
            }
            sr.Close();
        }



        public static bool SerializeSocialsIn(int inVersion)
        {
#if NO_SILVERLIGHT
            string szFName = EmlenMud.HostPathnameToMudDirectory + "\\" + MiscConstants.DataFileDirectory + "\\" + typeof(SocialData).Name.ToString() + inVersion.ToString();
            Object o = SerializationHelper.SerializeObjectIn(szFName);
            if (o == null) return false;
            if (o is ArrayList)
            {
                ArrayList rgIn = (ArrayList)o;
                EmlenMud.WorldTemplates.AllSocials.Clear();
                EmlenMud.WorldTemplates.htSocialsByName.Clear();
                foreach (SocialData social in rgIn)
                {
                    SocialData.AddSocial(social);
                }
                return true;
            }
            return false;
#else 
            return true;
#endif
        }

        public static bool SerializeSocialsOut(int outVersion)
        {
#if NO_SILVERLIGHT
            string szFName = EmlenMud.HostPathnameToMudDirectory + "\\" + MiscConstants.DataFileDirectory + "\\" + typeof(SocialData).Name.ToString() + outVersion.ToString();
            return SerializationHelper.SerializeObjectOut(EmlenMud.WorldTemplates.AllSocials, szFName, true);
#else 
            return true;
#endif
        }

        public static bool AddSocial(SocialData s)
        {
            //if (s.EmlenMud == null) s.AssignEmlenMud(EmlenMud);
            if (EmlenMud.WorldTemplates.htSocialsByName[s.socialname] == null)
            {
                EmlenMud.WorldTemplates.htSocialsByName[s.socialname] = s;
                EmlenMud.WorldTemplates.AllSocials.Add(s);
                return true;
            }
            return false;
        }

    }
}
